Trò chơi đua xe động vật trong UNITY Engine
114.638 lượt xem;
- ResultLayer.cs
- Result /
- GameScreen /
- AnimalRacing /
- Scripts /
- Assets /
- project /
1 using UnityEngine;
2 using System.Collections;
3
4 namespace GamePlay
5 {
6 public class ResultLayer : MonoBehaviour
7 {
8 public GameObject[] stars;
9 public GameObject[] buttons;
10 public Sprite starSprite;
11
12 private int score;
13 private int star;
14
15 private bool isShown;
16
17 public GameObject[] labels;
18 public GameObject scoreLabel;
19
20 private int m_gold;
21
22 void Start()
23 {
24 // ARController.showInterstitialAd();
25
26 //GoogleMobileAdControll.AdmobControll.ShowInterstitial();
27 }
28
29 void Update()
30 {
31 if (isShown)
32 {
33 createGUI();
34 if (star > 0)
35 {
36 saveData();
37 }
38 isShown = false;
39 }
40
41 if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Backspace))
42 {
43 SoundManager.LoadBgMusic("Sounds/menu", true);
44 Application.LoadLevel("MapScreen");
45 }
46 }
47
48 private void saveData()
49 {
50 int starSaved = Data.getData(Data.KEY_STAR + (Attr.currentWorld * 15 + Attr.currentLevel));
51 if (star > starSaved)
52 {
53 Data.saveData(Data.KEY_STAR + (Attr.currentWorld * 15 + Attr.currentLevel), star);
54 Data.saveData(Data.KEY_COIN, Data.getData(Data.KEY_COIN) + m_gold);
55
56 if (Attr.currentLevel == 14)
57 {
58 int worldSaved = Data.getData(Data.KEY_WORLD_MAP);
59 if (Attr.currentWorld == worldSaved - 1)
60 {
61 Data.saveData(Data.KEY_WORLD_MAP, worldSaved + 1);
62 }
63 }
64 else
65 {
66 int levelSaved = Data.getData(Data.KEY_LEVEL + Attr.currentWorld);
67 if (Attr.currentLevel == levelSaved - 1)
68 {
69 Data.saveData(Data.KEY_LEVEL + Attr.currentWorld, levelSaved + 1);
70 }
71 }
72 }
73 else
74 {
75 Data.saveData(Data.KEY_COIN, Data.getData(Data.KEY_COIN) + m_gold);
76 }
77 }
78
79 private void createGUI()
80 {
81 for (int i = 0; i < 3; i++)
82 {
83 if (i < star)
84 {
85 // stars[i].GetComponent<SpriteRenderer>().sprite = starSprite;
86 int temp = i;
87 stars[temp].GetComponent<Actor>().addAction(new ActionSequence(
88 new ActionDelay(0.4f + 0.6f * temp),
89 new ActionRunnable(delegate()
90 {
91 stars[temp].GetComponent<SpriteRenderer>().sprite = starSprite;
92
93 createStars(stars[temp].transform.localPosition, temp);
94 if (temp == star - 1)
95 {
96 showButtons(true);
97 }
98 })
99 ));
100 }
101 else
102 {
103 showButtons(false);
104 }
105 }
106 }
107
108 private void showButtons(bool showNext)
109 {
110 if (showNext)
111 {
112 for (int i = 0; i < 3; i++)
113 {
114 buttons[i].SetActive(true);
115 buttons[i].AddComponent<Actor>().addAction(new ActionScaleTo(1, 1, 0.5f, Interpolation.swingOut));
116 }
117 }
118 else
119 {
120 for (int i = 0; i < 2; i++)
121 {
122 buttons[i].SetActive(true);
123 buttons[i].AddComponent<Actor>().addAction(new ActionScaleTo(1, 1, 0.5f, Interpolation.swingOut));
124 }
125 buttons[2].SetActive(false);
126 }
127 }
128
129 private void createStars(Vector3 position, int starIndex)
130 {
131 for (int i = 0; i < 20; i++)
132 {
133 GameObject starObject = new GameObject("StarFly");
134 starObject.layer = LayerMask.NameToLayer("GUI");
135 starObject.transform.parent = gameObject.transform;
136 starObject.transform.localPosition = new Vector3(position.x, position.y, -3);
137 starObject.AddComponent<SpriteRenderer>().sprite = starSprite;
138 starObject.GetComponent<SpriteRenderer>().sortingLayerName = "GUI";
139 starObject.transform.localScale = new Vector3(0.5f, 0.5f, starObject.transform.localScale.z);
140
141 starObject.AddComponent<Actor>().addAction(new ActionParallel(
142 new ActionRotateBy(720, 2),
143 new ActionScaleTo(0, 0, 1.5f)
144 ));
145
146 if (starIndex == 0)
147 {
148 starObject.AddComponent<Bezier>().setBezier(-1,
149 new Vector2(position.x, position.y),
150 new Vector2(position.x, Random.Range(1f, 2f)),
151 new Vector2(position.x - Random.Range(2f, 3f), Random.Range(1f, 2f)),
152 new Vector2(position.x - Random.Range(2f, 5.5f), Random.Range(-2f, -1f))
153 );
154 }
155 else if (starIndex == 1)
156 {
157 starObject.AddComponent<Bezier>().setBezier(-1,
158 new Vector2(position.x, position.y),
159 new Vector2(position.x, Random.Range(1f, 2f)),
160 new Vector2(Random.Range(-1f, 1f), Random.Range(1f, 2f)),
161 new Vector2(Random.Range(-2f, 2f), Random.Range(-4f, -2f))
162 );
163 }
164 else if (starIndex == 2)
165 {
166 starObject.AddComponent<Bezier>().setBezier(-1,
167 new Vector2(position.x, position.y),
168 new Vector2(position.x, Random.Range(1f, 2f)),
169 new Vector2(position.x + Random.Range(2f, 3f), Random.Range(1f, 2f)),
170 new Vector2(position.x + Random.Range(2f, 5.5f), Random.Range(-4f, -2f))
171 );
172 }
173 Destroy(starObject, 2);
174 }
175 }
176
177
178 public void setParams(int score, int place, int gold, float time)
179 {
180 this.score = score;
181 this.m_gold = gold + getBonus(place);
182 isShown = true;
183 if (place == 1) star = 3;
184 else if (place > 1 && place <= 3) star = 2;
185 else if (place > 3 && place <= 5) star = 1;
186 else star = 0;
187
188 BitmapFont scoreFont = new BitmapFont("Fonts/font_result", "Fonts/font_result_xml", scoreLabel);
189 scoreFont.setText("" + score, 0, 0, "GUI", "GUI");
190 scoreLabel.transform.localPosition = new Vector3(scoreLabel.transform.localPosition.x - scoreFont.width/2,
191 scoreLabel.transform.localPosition.y, scoreLabel.transform.localPosition.z);
192
193 BitmapFont desFont = new BitmapFont("Fonts/shop_font", "Fonts/shop_font_xml", gameObject);
194
195 BitmapFont bonusFont = new BitmapFont(desFont, labels[0]);
196 bonusFont.setText("BONUS: " + getBonus(place), 0, 15, "GUI", "GUI");
197
198 BitmapFont timeFont = new BitmapFont(desFont, labels[1]);
199 timeFont.setText("TIME: " + ((int)(time * 100))/100f, 0, 15, "GUI","GUI");
200
201 BitmapFont placeFont = new BitmapFont(desFont, labels[2]);
202 placeFont.setText("PLACE: " + place, 0, 15, "GUI", "GUI");
203
204 BitmapFont goldFont = new BitmapFont(desFont, labels[3]);
205 goldFont.setText("GOLD: " + gold, 0, 15, "GUI", "GUI");
206 }
207
208 private int getBonus(int place)
209 {
210 if (place == 1) return 200;
211 else if (place > 1 && place <= 3) return 100;
212 else if (place > 3 && place <= 5) return 50;
213 else return 0;
214 }
215
216 public int getStar()
217 {
218 return star;
219 }
220 }
221 }
2 using System.Collections;
3
4 namespace GamePlay
5 {
6 public class ResultLayer : MonoBehaviour
7 {
8 public GameObject[] stars;
9 public GameObject[] buttons;
10 public Sprite starSprite;
11
12 private int score;
13 private int star;
14
15 private bool isShown;
16
17 public GameObject[] labels;
18 public GameObject scoreLabel;
19
20 private int m_gold;
21
22 void Start()
23 {
24 // ARController.showInterstitialAd();
25
26 //GoogleMobileAdControll.AdmobControll.ShowInterstitial();
27 }
28
29 void Update()
30 {
31 if (isShown)
32 {
33 createGUI();
34 if (star > 0)
35 {
36 saveData();
37 }
38 isShown = false;
39 }
40
41 if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Backspace))
42 {
43 SoundManager.LoadBgMusic("Sounds/menu", true);
44 Application.LoadLevel("MapScreen");
45 }
46 }
47
48 private void saveData()
49 {
50 int starSaved = Data.getData(Data.KEY_STAR + (Attr.currentWorld * 15 + Attr.currentLevel));
51 if (star > starSaved)
52 {
53 Data.saveData(Data.KEY_STAR + (Attr.currentWorld * 15 + Attr.currentLevel), star);
54 Data.saveData(Data.KEY_COIN, Data.getData(Data.KEY_COIN) + m_gold);
55
56 if (Attr.currentLevel == 14)
57 {
58 int worldSaved = Data.getData(Data.KEY_WORLD_MAP);
59 if (Attr.currentWorld == worldSaved - 1)
60 {
61 Data.saveData(Data.KEY_WORLD_MAP, worldSaved + 1);
62 }
63 }
64 else
65 {
66 int levelSaved = Data.getData(Data.KEY_LEVEL + Attr.currentWorld);
67 if (Attr.currentLevel == levelSaved - 1)
68 {
69 Data.saveData(Data.KEY_LEVEL + Attr.currentWorld, levelSaved + 1);
70 }
71 }
72 }
73 else
74 {
75 Data.saveData(Data.KEY_COIN, Data.getData(Data.KEY_COIN) + m_gold);
76 }
77 }
78
79 private void createGUI()
80 {
81 for (int i = 0; i < 3; i++)
82 {
83 if (i < star)
84 {
85 // stars[i].GetComponent<SpriteRenderer>().sprite = starSprite;
86 int temp = i;
87 stars[temp].GetComponent<Actor>().addAction(new ActionSequence(
88 new ActionDelay(0.4f + 0.6f * temp),
89 new ActionRunnable(delegate()
90 {
91 stars[temp].GetComponent<SpriteRenderer>().sprite = starSprite;
92
93 createStars(stars[temp].transform.localPosition, temp);
94 if (temp == star - 1)
95 {
96 showButtons(true);
97 }
98 })
99 ));
100 }
101 else
102 {
103 showButtons(false);
104 }
105 }
106 }
107
108 private void showButtons(bool showNext)
109 {
110 if (showNext)
111 {
112 for (int i = 0; i < 3; i++)
113 {
114 buttons[i].SetActive(true);
115 buttons[i].AddComponent<Actor>().addAction(new ActionScaleTo(1, 1, 0.5f, Interpolation.swingOut));
116 }
117 }
118 else
119 {
120 for (int i = 0; i < 2; i++)
121 {
122 buttons[i].SetActive(true);
123 buttons[i].AddComponent<Actor>().addAction(new ActionScaleTo(1, 1, 0.5f, Interpolation.swingOut));
124 }
125 buttons[2].SetActive(false);
126 }
127 }
128
129 private void createStars(Vector3 position, int starIndex)
130 {
131 for (int i = 0; i < 20; i++)
132 {
133 GameObject starObject = new GameObject("StarFly");
134 starObject.layer = LayerMask.NameToLayer("GUI");
135 starObject.transform.parent = gameObject.transform;
136 starObject.transform.localPosition = new Vector3(position.x, position.y, -3);
137 starObject.AddComponent<SpriteRenderer>().sprite = starSprite;
138 starObject.GetComponent<SpriteRenderer>().sortingLayerName = "GUI";
139 starObject.transform.localScale = new Vector3(0.5f, 0.5f, starObject.transform.localScale.z);
140
141 starObject.AddComponent<Actor>().addAction(new ActionParallel(
142 new ActionRotateBy(720, 2),
143 new ActionScaleTo(0, 0, 1.5f)
144 ));
145
146 if (starIndex == 0)
147 {
148 starObject.AddComponent<Bezier>().setBezier(-1,
149 new Vector2(position.x, position.y),
150 new Vector2(position.x, Random.Range(1f, 2f)),
151 new Vector2(position.x - Random.Range(2f, 3f), Random.Range(1f, 2f)),
152 new Vector2(position.x - Random.Range(2f, 5.5f), Random.Range(-2f, -1f))
153 );
154 }
155 else if (starIndex == 1)
156 {
157 starObject.AddComponent<Bezier>().setBezier(-1,
158 new Vector2(position.x, position.y),
159 new Vector2(position.x, Random.Range(1f, 2f)),
160 new Vector2(Random.Range(-1f, 1f), Random.Range(1f, 2f)),
161 new Vector2(Random.Range(-2f, 2f), Random.Range(-4f, -2f))
162 );
163 }
164 else if (starIndex == 2)
165 {
166 starObject.AddComponent<Bezier>().setBezier(-1,
167 new Vector2(position.x, position.y),
168 new Vector2(position.x, Random.Range(1f, 2f)),
169 new Vector2(position.x + Random.Range(2f, 3f), Random.Range(1f, 2f)),
170 new Vector2(position.x + Random.Range(2f, 5.5f), Random.Range(-4f, -2f))
171 );
172 }
173 Destroy(starObject, 2);
174 }
175 }
176
177
178 public void setParams(int score, int place, int gold, float time)
179 {
180 this.score = score;
181 this.m_gold = gold + getBonus(place);
182 isShown = true;
183 if (place == 1) star = 3;
184 else if (place > 1 && place <= 3) star = 2;
185 else if (place > 3 && place <= 5) star = 1;
186 else star = 0;
187
188 BitmapFont scoreFont = new BitmapFont("Fonts/font_result", "Fonts/font_result_xml", scoreLabel);
189 scoreFont.setText("" + score, 0, 0, "GUI", "GUI");
190 scoreLabel.transform.localPosition = new Vector3(scoreLabel.transform.localPosition.x - scoreFont.width/2,
191 scoreLabel.transform.localPosition.y, scoreLabel.transform.localPosition.z);
192
193 BitmapFont desFont = new BitmapFont("Fonts/shop_font", "Fonts/shop_font_xml", gameObject);
194
195 BitmapFont bonusFont = new BitmapFont(desFont, labels[0]);
196 bonusFont.setText("BONUS: " + getBonus(place), 0, 15, "GUI", "GUI");
197
198 BitmapFont timeFont = new BitmapFont(desFont, labels[1]);
199 timeFont.setText("TIME: " + ((int)(time * 100))/100f, 0, 15, "GUI","GUI");
200
201 BitmapFont placeFont = new BitmapFont(desFont, labels[2]);
202 placeFont.setText("PLACE: " + place, 0, 15, "GUI", "GUI");
203
204 BitmapFont goldFont = new BitmapFont(desFont, labels[3]);
205 goldFont.setText("GOLD: " + gold, 0, 15, "GUI", "GUI");
206 }
207
208 private int getBonus(int place)
209 {
210 if (place == 1) return 200;
211 else if (place > 1 && place <= 3) return 100;
212 else if (place > 3 && place <= 5) return 50;
213 else return 0;
214 }
215
216 public int getStar()
217 {
218 return star;
219 }
220 }
221 }
ARController.showInterstitialAd();
GoogleMobileAdControll.AdmobControll.ShowInterstitial();
stars[i].GetComponent